package com.renwei.yunlong.fragment;

import android.annotation.SuppressLint;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.fingdo.statelayout.StateLayout;
import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.adapter.WorkHistoryPagerAdapter;
import com.renwei.yunlong.base.BaseLazyFragment;
import com.renwei.yunlong.bean.HistoryBean;
import com.renwei.yunlong.event.WorkPageRefreshEvent;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.JsonMapUtil;
import com.renwei.yunlong.utils.ServiceRequestManager;
import com.renwei.yunlong.utils.StringUtils;

import java.util.HashMap;

import butterknife.Bind;
import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

/**
 * 工单  记录  页面
 * <p>
 * Created by Administrator on 2017/9/6.
 */

public class WorkRecordingFragment extends BaseLazyFragment implements HttpTaskListener {

    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.state_layout)
    StateLayout stateLayout;

    private String requestId;
    private String outsourceFlag;
    private WorkHistoryPagerAdapter workHistoryAdapter;

    @SuppressLint("ValidFragment")
    public WorkRecordingFragment(String requestId) {
        this.requestId = requestId;
    }

    @SuppressLint("ValidFragment")
    public WorkRecordingFragment(String requestId, String outsourceFlag) {
        this.requestId = requestId;
        this.outsourceFlag =outsourceFlag;
    }

    public WorkRecordingFragment() {
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_work_process_pager;
    }

    @Override
    public void onLazyLoad() {
        EventBus.getDefault().register(this);
        initView();
        initData();
    }


    public void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        workHistoryAdapter = new WorkHistoryPagerAdapter(getContext());
        recyclerView.setAdapter(workHistoryAdapter);
    }

    /**
     * 当前刷新
     */
    @Subscribe(threadMode = ThreadMode.MainThread)
    public void onRefresh(WorkPageRefreshEvent event) {
        initData();
    }

    public void initData() {
        HashMap<String, String> map = new HashMap<>();
        if ("1".equals(companyType)) {
            map.put("relatId",requestId);
            map.put("currentUserId",ownerBean.getRows().getEmployeeId());
        } else {
            map.put("relatId",requestId);
            map.put("currentUserId",serviceLoginBean.getRows().getEmployeeId());
        }
        ServiceRequestManager.getManager().getWorkRecording(getContext(), StringUtils.value(outsourceFlag), JsonMapUtil.mapToJson(map),this);
    }

    @Override
    public void onSuccess(int requestId, String result) {
        HistoryBean bean = new Gson().fromJson(result, HistoryBean.class);
        if (bean.getMessage().getCode() == 200) {
            workHistoryAdapter.setData(bean.getRows());
            if (bean.getRows()==null||bean.getRows().size() == 0) {
                stateLayout.showEmptyView();
            } else {
                stateLayout.showContentView();
            }
        }
    }

    @Override
    public void onException(int requestId, String code) {
        stateLayout.showErrorView();
    }
}
